package com.wtwd.device.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.device.dao.AppConfigurationMapper;
import com.wtwd.device.model.dto.AppConfigurationDTO;
import com.wtwd.device.model.dto.EmailConfiguration;
import com.wtwd.device.model.dto.SMSConfiguration;
import com.wtwd.device.model.entity.AppConfiguration;
import com.wtwd.device.service.AppConfigurationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zdl
 * @date 2023/10/25 15:59
 */
@Service
public class AppConfigurationServiceImpl implements AppConfigurationService {

    private final static Map<Long, AppConfigurationDTO> CACHE = new HashMap<>();


    @Resource
    private AppConfigurationMapper appConfigurationMapper;

    @Override
    public AppConfigurationDTO getById(Long appId) {
        if (appId == null) {
            appId = 1L;
        }
        AppConfigurationDTO data = CACHE.get(appId);
        if (data == null) {
            AppConfiguration appConfiguration = appConfigurationMapper.selectById(appId);
            data = toDTO(appConfiguration);
            CACHE.put(appId, data);
        }
        return data;
    }


    @Override
    public void add(AppConfigurationDTO appConfigurationDTO) {
        AppConfiguration appConfiguration = toEntity(appConfigurationDTO);
        appConfigurationMapper.insert(appConfiguration);

    }

    private AppConfigurationDTO toDTO(AppConfiguration appConfiguration) {
        AppConfigurationDTO appConfigurationDTO = new AppConfigurationDTO();
        appConfigurationDTO.setId(appConfiguration.getId());
        appConfigurationDTO.setName(appConfiguration.getName());
        appConfigurationDTO.setUserTable(appConfiguration.getUserTable());
        appConfigurationDTO.setSmsConfiguration(JSONObject.parseObject(appConfiguration.getSms(), SMSConfiguration.class));
        appConfigurationDTO.setEmailConfiguration(JSONObject.parseObject(appConfiguration.getEmail(), EmailConfiguration.class));
        return appConfigurationDTO;

    }

    private AppConfiguration toEntity(AppConfigurationDTO appConfigurationDTO) {
        AppConfiguration appConfiguration = new AppConfiguration();
        appConfiguration.setName(appConfigurationDTO.getName());
        appConfiguration.setUserTable(appConfigurationDTO.getUserTable());
        appConfiguration.setSms(JSONObject.toJSONString(appConfigurationDTO.getSmsConfiguration()));
        appConfiguration.setEmail(JSONObject.toJSONString(appConfigurationDTO.getEmailConfiguration()));
        return appConfiguration;

    }

    @Override
    public void update(AppConfigurationDTO appConfigurationDTO) {
        AppConfiguration appConfiguration = toEntity(appConfigurationDTO);
        appConfigurationMapper.updateById(appConfiguration);
        CACHE.put(appConfiguration.getId(), appConfigurationDTO);
    }

    @Override
    public SMSConfiguration getSMSConfigurationByAppId(Long appId) {
        return getById(appId).getSmsConfiguration();
    }

    @Override
    public EmailConfiguration getEmailConfigurationByAppId(Long appId) {
        return getById(appId).getEmailConfiguration();
    }
}
